package com.zood.controller;

import com.zood.controller.utils.R;
import com.zood.dao.TitleDao;
import com.zood.domain.result;
import com.zood.domain.title;
import com.zood.service.resultService;
import com.zood.service.scaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/scale")
public class scaleController {
    @Autowired
    private scaleService Service;

    @Autowired
    private resultService Service1;

    @Autowired
    private TitleDao Title;

    @GetMapping("{type}")
    public R getByType(@PathVariable Integer type){
        if(type.equals(0)){
            return  new R(true, Service.getAll());
        }else{
            title title_name = Title.selectById(type);
            String questionName=title_name.getTitle();
            return new R(true, Service.getAllByType(type),questionName);
        }
    }

    @GetMapping
    public R getAll(){
        return new R(true, Service.getAll());
    }

    @PostMapping
    public R save(@RequestBody result activity) throws IOException {
        boolean flag = Service1.saveResult(activity);
        return new R(flag, flag ? "添加成功^_^" : "添加失败-_-!");
    }
}
